#include "stdafx.h"
#include <fstream>
#include <vector>

HSTile::HSTile()
{
	sId = "error";
	uiSizeX = 0;
	uiSizeY = 0;
	sTex = "";
}

HSObject::HSObject()
{
	sId = "error";
	uiSizeX = 0;
	uiSizeY = 0;
	uiBlockRadius = 0;
	sTex = "";
	uiFrameCount = 0;
	fAnimDelay = 0.0f;
	fAnimPause = 0.0f;
	bProximityTrigger = false;
}

typedef std::map<std::string, HSTile> HSTileMap;
typedef std::map<std::string, HSObject> HSObjectMap;

static HSTileMap mapTiles;
static HSObjectMap mapObjects;

const HSTile* const GetTile( const std::string& sId )
{
	HSTileMap::const_iterator it = mapTiles.find( sId );

	if ( it != mapTiles.end() )
	{
		return &it->second;
	}

	return NULL;
}

const HSObject* const GetObject( const std::string& sId )
{
	HSObjectMap::const_iterator it = mapObjects.find( sId );

	if ( it != mapObjects.end() )
	{
		return &it->second;
	}

	return NULL;
}

bool LoadBlueprints( const std::string& sBlueprintsFile )
{
	mapTiles.clear();
	mapObjects.clear();

	std::ifstream in( "Resources/Data/" + sBlueprintsFile, std::ifstream::in );

	if ( !in.is_open() )
	{
		sysLog.Printf( "Failed to open %s", sBlueprintsFile.c_str() );
		return false;
	}

	// tile id sizeX sizeY texture
	// object id sizeX sizeY blockRadius texture
	//		   frameCount animDelay animPause yes/no
		
	while ( !in.eof() )
	{
		std::string sType = "";
		in >> sType;

		if ( in.fail() )
		{
			sysLog.Printf( "Failed to parse data from %s", sBlueprintsFile.c_str() );
			return false;
		}

		if ( sType == "tile" )
		{
			// format: tile id sizeX sizeY texture

			// make sure no tile is defined twice
			std::string sId = "error";
			in >> sId;

			if ( mapTiles.find( sId ) != mapTiles.end() )
			{
				sysLog.Printf( "[LoadBlueprints] Tile '%s' is already defined!", sId.c_str() );
				return false;
			}
			
			mapTiles[ sId ].sId = sId;

			in >> mapTiles[ sId ].uiSizeX >> mapTiles[ sId ].uiSizeY >> mapTiles[ sId ].sTex;
		}
		else if ( sType == "object" )
		{
			// format: object id sizeX sizeY blockRadius texture
			//				  frameCount animDelay animPause yes/no

			// make sure no tile is defined twice
			std::string sId = "error";
			in >> sId;

			if ( mapObjects.find( sId ) != mapObjects.end() )
			{
				sysLog.Printf( "[LoadBlueprints] Object '%s' is already defined!", sId.c_str() );
				return false;
			}
			
			mapObjects[ sId ].sId = sId;

			in >> mapObjects[ sId ].uiSizeX >> mapObjects[ sId ].uiSizeY >> mapObjects[ sId ].uiBlockRadius;
			in >> mapObjects[ sId ].sTex >> mapObjects[ sId ].uiFrameCount;
			in >> mapObjects[ sId ].fAnimDelay >> mapObjects[ sId ].fAnimPause;

			std::string sYesNo = "no";

			in >> sYesNo;

			if ( sYesNo == "yes" )
			{
				mapObjects[ sId ].bProximityTrigger = true;
			}
			else
			{
				mapObjects[ sId ].bProximityTrigger = false;
			}

			std::string sActionKeyword;

			in >> sActionKeyword >> mapObjects[ sId ].sActionType;

			if ( sActionKeyword == "action" )
			{
				if ( mapObjects[ sId ].sActionType == "dialog" )
				{
					in >> mapObjects[ sId ].sActionArg;
				}
			}
			else
			{
				sysLog.Log( "[LoadBlueprints] missing action keyword" );
				return false;
			}
		}
		else
		{
			sysLog.Printf( "[LoadBlueprints] Unrecognized type '%s' encountered!", sType.c_str() );
			return false;
		}

	}

	return true;
}
